<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;

class LogContext
{
    /**
     * Handle an incoming request.
     *
     * @param \Closure(\Illuminate\Http\Request): (Response) $next
     * @param Request                                        $request
     */
    public function handle(Request $request, Closure $next): Response
    {
        $requestId = Arr::get(Log::sharedContext(), 'request_id');

        return $next($request)->header('Request-Id', $requestId);
    }
}
